import 'package:flutter/material.dart';
import 'package:easy_refresh/easy_refresh.dart';

class ZSmartRefresh extends StatelessWidget {
  final EasyRefreshController controller;
  final Widget child;
  final bool showNoDataWidget;
  final String noDataMsg;
  final Widget? noDataWidget;
  final String? loadMoreNoDataText;
  final bool enableRefresh;
  final bool enableLoad;
  final Function()? onRefresh;
  final Function()? onLoad;
  final Color? bgColor;
  final bool lightText;

  const ZSmartRefresh({
    required this.controller,
    required this.child,
    this.showNoDataWidget = false,
    this.noDataMsg = '暂无数据',
    this.noDataWidget,
    this.loadMoreNoDataText,
    this.enableRefresh = true,
    this.enableLoad = true,
    this.onRefresh,
    this.onLoad,
    this.bgColor,
    this.lightText = false,
  });

  @override
  Widget build(BuildContext context) {
    Widget sr = EasyRefresh(
      controller: controller,
      child: showNoDataWidget ? (noDataWidget == null ? Center(child: Text(noDataMsg)) : noDataWidget) : child,
      header: ClassicHeader(
        textStyle: TextStyle(color: lightText ? Colors.white70 : Colors.grey),
        clamping: false,
        backgroundColor: Colors.transparent,
        mainAxisAlignment: MainAxisAlignment.center,
        showMessage: true,
        showText: true,
        infiniteOffset: null,
        triggerWhenReach: false,
        dragText: '下拉刷新',
        armedText: '释放开始',
        readyText: '刷新中...',
        processingText: '刷新中...',
        processedText: '成功了',
        noMoreText: '没有更多',
        failedText: '失败了',
        messageText: '最后更新于 %T',
      ),
      footer: ClassicFooter(
        textStyle: TextStyle(color: lightText ? Colors.white70 : Colors.grey),
        clamping: false,
        backgroundColor: Colors.transparent,
        mainAxisAlignment: MainAxisAlignment.start,
        showMessage: true,
        showText: true,
        infiniteOffset: 0,
        triggerWhenReach: false,
        dragText: '上拉加载',
        armedText: '释放开始',
        readyText: '刷新中...',
        processingText: '刷新中...',
        processedText: '成功了',
        noMoreText: '没有更多',
        failedText: '失败了',
        messageText: '最后更新于 %T',
      ),
      notRefreshHeader: const NotRefreshHeader(maxOverOffset: 0),
      notLoadFooter: const NotLoadFooter(maxOverOffset: 0),
      // enablePullDown: enableRefresh,
      // enablePullUp: enableLoad,
      onRefresh: enableRefresh ? onRefresh : null,
      onLoad: enableLoad ? onLoad : null,
    );
    return bgColor == null
        ? sr
        : Container(
            color: bgColor,
            child: sr,
          );
  }
}
